<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class email_api{

	public function sendTomem($type,$uid){
		log_message('debug',"email_api sendTomem=$type uid=$uid");
		if(!in_array($type, array('register','findpassword','register','registerverify'))){
			return;
		}
		$pz = &get_instance();
		$user = load_api('user')->getUserByid($uid);
		if($type=='register'){
			log_message('debug',"email_api send register uid=$uid");
			$subject = lang('subject_register_thanks','email');
			$message = $pz->render('email/registerthanks', array('nickname'=>$user['nickname'],'email'=>$user['email']));
		}elseif($type=='findpassword'){
			log_message('debug',"email_api send register uid=$uid");
			$subject = lang('subject_find_password','email');
			$reseturl = site_url('user/resetpassword?passkey='.$user['lost_password_key']);
			$message = $pz->render('email/findpassword', array('nickname'=>$user['nickname'],'email'=>$user['email'],'user'=>$user));
		}elseif($type=='registerverify'){
			log_message('debug',"email_api send registerverify uid=$uid");
			$subject = lang('subject_register_verify','email');
			$message = $pz->render('email/registerverify', array('nickname'=>$user['nickname'],'email'=>$user['email'],'activelink'=>host_url(site_url('user/validemail?hash='.pzxencode($uid.'-'.$user['create_time'])))));
		}
		if($message){
			log_message('debug','$subject='.$subject);
			$this->send($subject,$user['email'],$message);
		}
	}


	public function send($subject,$to_email,$message){
		$api = load_api('setting');
		$config = $api->getSetting('email');
		$config['mailtype']='html';

		$email_sender = &load_class('Email');
		$email_sender->initialize($config);
		$email_sender->set_newline("\r\n");
		$email_sender->from($config['from'], $config['sender']);
		$email_sender->to($to_email);
		$email_sender->subject($subject);
		$email_sender->message($message);
		$email_sender->send();
	}

	function asyn_sendmail($type,$uid){
		log_message('debug',"asyn_sendmail type=$type uid=$uid");
		$this->asyncRequest(host_url(site_url('email/sendTomem')), 'post', array('type'=>$type,'uid'=>$uid));
	}

	function asyn_sendtoadmin($type,$data){
		log_message('debug',"asyn_sendtoadmin type=$type");
		$data['type'] = $type;
		$this->asyncRequest(host_url(site_url('email/sendToadmin?XDEBUG_SESSION_START=ECLIPSE_DBGP&KEY=13851650220911')), 'post', $data);
	}

	function adminsendtest($data){
		$this->asyncRequest(host_url(site_url('email/admintest')), 'post', $data);
	}

	function asyncRequest($requestUrl, $type='get', $data=array()) {
		$url   = parse_url($requestUrl);
		$host  = $url['host']=='localhost'?'127.0.0.1':$url['host'];
		$path  = $url['path'];
		$query = $url['query'];
		$path .= $query ? '?'. $query : '';

		$parameter = $sep = '';
		if (!empty($data)) {
			foreach ($data as $key => $value){
				$parameter .= $sep . urlencode($key) .'='. urlencode($value);
				$sep = '&';
			}
		}
		if (strtolower($type) == 'get') {
			$path .= $parameter;
			$out  = "GET {$path} HTTP/1.1\r\n";
			$out .= "Accept: */*\r\n";
			$out .= "Accept-Language: zh-cn\r\n";
			$out .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
			$out .= "Host: {$host}\r\n";
			$out .= "Connection: Close\r\n\r\n";
		} else {
			$out  = "POST {$path} HTTP/1.1\r\n";
			$out .= "Accept: */*\r\n";
			$out .= "Accept-Language: zh-cn\r\n";
			$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
			$out .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
			$out .= "Host: {$host}\r\n";
			$out .= 'Content-Length: '.strlen($parameter)."\r\n";
			$out .= "Connection: Close\r\n\r\n";
			$out .= $parameter;
		}
		log_message('debug',"email_api before fsockopen=$requestUrl");
		$fp = fsockopen($host, 80, $errno, $errstr, 30);
		log_message('debug',"email_api after fp=$fp errno=$errno errstr=$errstr");
		if ($fp) {
			log_message('debug',"email_api after fsockopen=$host");
			fwrite($fp, $out);
			fclose($fp);
		}
	}
}